using System;
using System.IO;
using System.Windows.Forms;
using System.Data;

namespace IPAMS.Views
{
    /// <summary>
    /// This is an editor for the InvestmentHouses table. It enables loading from a file,
    ///saving to a file and more
    /// </summary>
    public partial class InvestmentHousesEditor : UserControl
    {
        public DSIPAMS.DTInvestmentManagementHousesDataTable houses;
        string[] investmentHousesHeaders;

        /// <summary>
        /// Initializes a new instance of the <see cref="InvestmentHousesEditor"/> class.
        /// </summary>
        public InvestmentHousesEditor()
        {
            InitializeComponent();
            InitializeComponentExtended();
        }

        /// <summary>
        /// Initializes the component extended.
        /// </summary>
        private void InitializeComponentExtended()
        {
            houses = new DSIPAMS.DTInvestmentManagementHousesDataTable();
        }

        /// <summary>
        /// Handles the DataSourceChanged event of the dgvInvestmentsHouses control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void dgvInvestmentsHouses_DataSourceChanged(object sender, EventArgs e)
        {
            dgvInvestmentsHouses.Refresh();
        }

        /// <summary>
        /// Loads the data from a file.
        /// </summary>
        /// <param name="dataFilePath">The data file path.</param>
        /// <returns>a DataTable with the file's data</returns>
        public DataTable LoadData(string dataFilePath)
        {


            if (File.Exists(dataFilePath))
            {
                StreamReader sr = new StreamReader(dataFilePath);
                investmentHousesHeaders = sr.ReadLine().Split(',');
                while (true)
                {
                    string house = sr.ReadLine();
                    if (house == string.Empty || house == null || house== @",,") break;
                    string[] houseInfo = house.Split(',');
                    if (houseInfo.Length != investmentHousesHeaders.Length) continue;
                    houses.AddDTInvestmentManagementHousesRow(houseInfo[0], houseInfo[1], houseInfo[2]);
                }
                sr.Close();

                houses.AcceptChanges();
                dgvInvestmentsHouses.DataSource = houses;
                Text = dataFilePath;
                for (int index = 0; index < investmentHousesHeaders.Length; index++)
                {
                    dgvInvestmentsHouses.Columns[index].HeaderText = investmentHousesHeaders[index];
                }
                foreach (DataGridViewColumn column in dgvInvestmentsHouses.Columns)
                {
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                }
            }


            return houses;
        }

        /// <summary>
        /// Saves the data to a file.
        /// </summary>
        /// <param name="dataFilePath">The data file path.</param>
        public void SaveData(string dataFilePath)
        {
            StreamWriter sw = new StreamWriter(dataFilePath);
            string fileContent = investmentHousesHeaders + Environment.NewLine;
            foreach (DSIPAMS.DTInvestmentManagementHousesRow house in houses)
            {
                fileContent += string.Format("{0},{1}" + Environment.NewLine, house[0], house[1]);
            }
            sw.Write(fileContent);
            sw.Close();

        }
    }
}
